<?php
/*
 * Created by PhpStorm.
 * User: yezhangtao
 * Date: 2020/11/23
 * Time: 10:48
*/

namespace App\Http\Controllers\Sites;


use App\Http\Controllers\Controller;
use App\User;
use GuzzleHttp\Client;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;

class BaseController extends Controller
{
    public $apiUser = null;
    public function __construct()
    {
        $this->apiUser = Auth::guard('sites')->user();
        if ($this->apiUser){
            return $this->apiUser = json_decode($this->apiUser, true);
        }
        return $this->apiUser;
    }
    /**
     * 密码登录
     * http://xxxx/sites/login?username=taotao&password=123456
     * Created by PhpStorm.
     * User: yezhangtao
     * Date: 2020/11/23
     * Time: 10:49
     * @param void
     * @param Request $request
     */
    public function login(Request $request){
        $http = new Client();
        $username = $request->get('username', '');
        $password = $request->get('password', '');
        try {
            $client = DB::table('oauth_clients')->where('name', 'sites_api')->where('id', 3)->first();
            if (!$client)
                return $this->errorResponse("登录类型不存在");

            $username = $request->input("username", "");
            $password = $request->input("password", "");
            if (empty($username) || empty($password))
                return $this->errorResponse("请输入用户名和密码");
            $user = User::where('username', $username)->first();
            if (!$user) {
                return $this->errorResponse('用户不存在');
            }
            $response = $http->post(env('APP_URL', 'http://laravel7.cnn:81').'/sites/oauth/token', [
                'form_params' => [
                    'grant_type' => 'password',
                    'client_id' => $client->id,
                    'client_secret' => $client->secret,
                    'username' => $username,
                    'password' => $password,
                    'scope' => '*',
                ],
            ]);
        }catch (\Exception $e){
            if ($e->getCode() == 401){
                return $this->errorJson('授权失败');
            }
            return $this->errorJson('登录过期，请重新登录');
        }
        return json_decode((string) $response->getBody(), true);
        }

    /**
     * 成功结果集
     * Created by PhpStorm.
     * User: yezhangtao
     * Date: 2020/11/23
     * Time: 10:53
     * @param \Illuminate\Http\JsonResponse
     * @param string $msg
     * @param int $code
     * @param array $data
     * @return \Illuminate\Http\JsonResponse
     */
    public function successJson($msg='', $code=1, $data=[]){
        return response()->json([
            'msg' => $msg,
            'code' => $code,
            'data' => $data
        ]);
    }

    /**
     * 失败结果集
     * Created by PhpStorm.
     * User: yezhangtao
     * Date: 2020/11/23
     * Time: 10:53
     * @param \Illuminate\Http\JsonResponse
     * @param string $msg
     * @param int $code
     * @param array $data
     * @return \Illuminate\Http\JsonResponse
     */
    public function errorJson($msg='', $code=0, $data=[]){
        return response()->json([
            'msg' => $msg,
            'code' => $code,
            'data' => $data
        ]);
    }
}
